package com.example.hospitalapp;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;

public class ApiClient {
    private Context context;

    public ApiClient(Context context) {
        this.context = context;
    }

    // GET 请求
    public void getRequest(String url, final VolleyCallback callback) {
        RequestQueue queue = Volley.newRequestQueue(context);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        callback.onSuccess(response);
                    }
                },
                error -> {

                }
        );

        queue.add(jsonObjectRequest);
    }

    // POST 请求
    public void postRequest(String url, JSONObject params, final VolleyCallback callback) {
        // 创建请求
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        callback.onSuccess(response);  // 成功回调
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // 错误日志输出
                        if (error instanceof NetworkError) {
                            Log.e("API_ERROR", "Network Error: " + error.toString());
                        } else if (error instanceof ServerError) {
                            Log.e("API_ERROR", "Server Error: " + error.toString());
                        } else if (error instanceof AuthFailureError) {
                            Log.e("API_ERROR", "Authentication Failure: " + error.toString());
                        } else if (error instanceof TimeoutError) {
                            Log.e("API_ERROR", "Timeout Error: " + error.toString());
                        } else {
                            Log.e("API_ERROR", "Other Error: " + error.toString());
                        }
                    }
                }
        );

        // 添加请求到请求队列
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(jsonObjectRequest);
    }
    public void putRequest(String url, JSONObject params, final VolleyCallback callback) {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.PUT, url, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        callback.onSuccess(response);
                    }
                },
                error -> {
                    // 错误日志输出
                    if (error instanceof NetworkError) {
                        Log.e("API_ERROR", "Network Error: " + error.toString());
                    } else if (error instanceof ServerError) {
                        Log.e("API_ERROR", "Server Error: " + error.toString());
                    } else if (error instanceof AuthFailureError) {
                        Log.e("API_ERROR", "Authentication Failure: " + error.toString());
                    } else if (error instanceof TimeoutError) {
                        Log.e("API_ERROR", "Timeout Error: " + error.toString());
                    } else {
                        Log.e("API_ERROR", "Other Error: " + error.toString());
                    }
                }
        );

        RequestQueue requestQueue = Volley.newRequestQueue(context);
        requestQueue.add(jsonObjectRequest);
    }

    // Callback接口
    public interface VolleyCallback {
        void onSuccess(JSONObject result);

        void onFailure(String error);
    }
}
